<?php

namespace App\Http\Controllers\Payment;

use App\Http\Controllers\Controller;
use App\Models\Common\ApiResponse;
use App\Models\Invoice\Title;
use Illuminate\Http\Request;

class InvoiceTitleController extends Controller
{

    public $needLogging = false;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $appUser = appUser();
        $baseQuery = Title::where('app_user_id', $appUser->id);
        if (is_integer($type = $request->get('type'))) {
            $baseQuery->where('type', $type);
        }
        return ApiResponse::return('', $baseQuery->get());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $appUser = appUser();

        if ($titleID = $request->get('id')) {
            $title = Title::findOrFail($titleID);
        } else {
            $title = new Title();
            $title->app_user_id = $appUser->id;
        }

        $title->fill($request->only(['type', 'is_default']));

        if ($title->type === Title::TYPE_COMPANY) {
            $value = $request->only(['title', 'phone', 'address', 'tax_code', 'bank_account']);

            if (!\Arr::get($value, 'title')) {
                throw new \InvalidArgumentException('开票抬头必填');
            }

            if (!\Arr::get($value, 'phone')) {
                throw new \InvalidArgumentException('开票联系方式必填');
            }

            if (!\Arr::get($value, 'address')) {
                throw new \InvalidArgumentException('开票地址必填');
            }

            if (!\Arr::get($value, 'tax_code')) {
                throw new \InvalidArgumentException('纳税人识别号必填');
            }

            if (!\Arr::get($value, 'bank_account')) {
                throw new \InvalidArgumentException('银行账号必填');
            }
        } else {
            $title->type = Title::TYPE_PERSONAL;
            $value = $request->only(['title', 'phone']);

            if (!\Arr::get($value, 'title')) {
                throw new \InvalidArgumentException('开票抬头必填');
            }

            if (!\Arr::get($value, 'phone')) {
                throw new \InvalidArgumentException('开票联系方式必填');
            }
        }

        $title->value = $value;

        if ($title->save()) {
            if ($title->is_default) {
                Title::where('app_user_id', $title->app_user_id)->where('id', '<>', $title->id)->update(['is_default' => false]);
            }
            return ApiResponse::return();
        }
        return ApiResponse::return('', null, ApiResponse::RESP_CODE_FAILURE);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show(string $id)
    {
        $appUser = appUser();
        $title = Title::where('app_user_id', $appUser->id)->findOrFail($id);
        return ApiResponse::return('', $title);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(string $id)
    {
        $appUser = appUser();
        if ($title = Title::where('app_user_id', $appUser->id)->find($id)) {
            $isDefault = $title->is_default;
            Title::where('app_user_id', $appUser->id)->where('id', $id)->delete();
            if ($isDefault) {
                if ($otherTitle = Title::where('app_user_id', $appUser->id)->first()) {
                    $otherTitle->is_default = true;
                    $otherTitle->save();
                }
            }
        }
        return ApiResponse::return();
    }
}
